#!/bin/bash

# vim: tabstop=4 shiftwidth=4 softtabstop=4
# -*- sh-basic-offset: 4 -*-

set -exuo pipefail

DEVICE=linux-lubancat-g++
SRC_PATH=/opt
BUILD_TARGET=/build

#Qt源码包名称，5.15版本使用qt-everywhere-opensource-src，其他的是qt-everywhere-src
#Qt源码包/交叉编译器解压后的名称，默认使用5.15.8版本
PACKAGE_NAME=qt-everywhere-src-5.15.8
PACKAGE_NAME_GCC=gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu

#定义编译后安装--生成的文件,文件夹位置路径
HOSTPREFIX_PATH=${BUILD_TARGET}/lubancat_qt5.15.8_eglfs/host
EXTPREFIX_PATH=${BUILD_TARGET}/lubancat_qt5.15.8_eglfs/ext

#定义sysroot目录,需根据自己实际放的位置确认，默认设置在/sysroot下
SYSROOT_PATH=${SRC_PATH}/sysroot

#添加交叉编译工具链路径，根据前面交叉编译器安装的路径设置
CROSS_CHAIN_PREFIX=${SRC_PATH}/${PACKAGE_NAME_GCC}/bin/aarch64-linux-gnu-

#定义压缩包名称
COMPRESS_PACKAGE=qt-everywhere-opensource-src.tar.xz
COMPRESS_PACKAGE_GCC=gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu.tar.xz

#自动组合下载地址
DOWNLOAD_LINK=http://download.qt.io/official_releases/qt/5.15/5.15.8/single/${COMPRESS_PACKAGE}
DOWNLOAD_LINK_GCC=https://armkeil.blob.core.windows.net/developer/Files/downloads/gnu-a/8.3-2019.03/binrel/${COMPRESS_PACKAGE_GCC}

#无需修改--自动组合平台路径
CONFIG_PATH=${SRC_PATH}/${PACKAGE_NAME}/qtbase/mkspecs/devices/${DEVICE}

#无需修改--自动组合配置平台路径文件
CONFIG_FILE=${CONFIG_PATH}/qmake.conf

#下载编译器、QT源码包并解压
fetch_src () {
	echo "start get src ..."
	pushd ${SRC_PATH}
	
	if [ ! -d "${PACKAGE_NAME}" ];then
		wget -c ${DOWNLOAD_LINK}
		tar -xf ${COMPRESS_PACKAGE}
	fi

	if [ ! -d "${PACKAGE_NAME_GCC}" ];then
		wget -c ${DOWNLOAD_LINK_GCC}
		tar -xf ${COMPRESS_PACKAGE_GCC}
	fi

	echo "done..."
	popd
}

#修理一些bug
patch_qt5 () {

    echo "patch qt ..."
    pushd ${SRC_PATH}/${PACKAGE_NAME}

    sed -i 's/False/True/' qtwebengine/src/3rdparty/chromium/third_party/catapult/tracing/tracing_build/generate_about_tracing_contents.py

	echo "done..."
    popd
}

#创建和修改配置平台
do_config_before () {
	echo "start configure platform..."
	pushd ${SRC_PATH}/${PACKAGE_NAME}
	
	if [ ! -d "${CONFIG_PATH}" ];then
		cp -a ${SRC_PATH}/${PACKAGE_NAME}/qtbase/mkspecs/devices/linux-generic-g++ ${CONFIG_PATH}
	fi

	echo "#" > ${CONFIG_FILE}
	echo "# qmake configuration for the LubanCat running , Linux for debian10 " >> ${CONFIG_FILE}
	echo "#" >> ${CONFIG_FILE}
	echo "" >> ${CONFIG_FILE}
	echo "include(../common/linux_device_pre.conf)" >> ${CONFIG_FILE}
	echo "" >> ${CONFIG_FILE}
	echo "QMAKE_LIBS_EGL         += -lEGL -lmali" >> ${CONFIG_FILE}
	echo "QMAKE_LIBS_OPENGL_ES2  += -lGLESv2 -lEGL -lmali" >> ${CONFIG_FILE}
	echo "QMAKE_CFLAGS            = -march=armv8-a" >> ${CONFIG_FILE}
	echo "QMAKE_CXXFLAGS          = \$\$QMAKE_CFLAGS" >> ${CONFIG_FILE}
	echo "QMAKE_LFLAGS += -static-libstdc++ -ldbus-1" >> ${CONFIG_FILE}
	echo "" >> ${CONFIG_FILE}
	
	echo "QMAKE_INCDIR_POST += \
		\$\$[QT_SYSROOT]/usr/include \
		\$\$[QT_SYSROOT]/usr/include/\$\${GCC_MACHINE_DUMP} " >> ${CONFIG_FILE}

	echo "QMAKE_LIBDIR_POST += \
		\$\$[QT_SYSROOT]/usr/lib \
		\$\$[QT_SYSROOT]/lib/\$\${GCC_MACHINE_DUMP} \
		\$\$[QT_SYSROOT]/usr/lib/\$\${GCC_MACHINE_DUMP} " >> ${CONFIG_FILE}

	echo "QMAKE_RPATHLINKDIR_POST += \
		\$\$[QT_SYSROOT]/usr/lib \
		\$\$[QT_SYSROOT]/usr/lib/\$\${GCC_MACHINE_DUMP} \
		\$\$[QT_SYSROOT]/lib/\$\${GCC_MACHINE_DUMP} " >> ${CONFIG_FILE}
	
	echo "" >> ${CONFIG_FILE}
	echo "DISTRO_OPTS += aarch64" >> ${CONFIG_FILE}
	echo "DISTRO_OPTS += deb-multi-arch" >> ${CONFIG_FILE}
	echo "" >> ${CONFIG_FILE}
	echo "include(../common/linux_arm_device_post.conf)" >> ${CONFIG_FILE}
	echo "load(qt_config)" >> ${CONFIG_FILE}

	cat ${CONFIG_FILE}
	echo "done..."
	popd
}

#配置选项
do_configure () {
	echo "start configure ${PACKAGE_NAME}..."
	pushd ${SRC_PATH}/${PACKAGE_NAME}
	
	./configure \
	-sysroot ${SYSROOT_PATH}  \
	-hostprefix ${HOSTPREFIX_PATH} \
	-extprefix ${EXTPREFIX_PATH} \
	-device ${DEVICE} \
	-device-option CROSS_COMPILE=${CROSS_CHAIN_PREFIX} \
	-release \
	-opensource \
	-confirm-license \
	-make libs \
	-opengl es2 \
	-eglfs \
	-qpa eglfs \
	-qt-pcre \
	-evdev \
	-pkg-config \
	-no-compile-examples \
	-no-cups \
	-no-gbm \
	-no-gtk \
	-no-use-gold-linker \
    -no-xcb \
	-nomake examples \
	-nomake tests \
	-skip qt3d \
	-skip qtactiveqt \
	-skip qtscript \
	-skip qtandroidextras \
	-skip qtcanvas3d \
	-skip qtcharts \
	-skip qtdatavis3d \
	-skip qtgamepad \
	-skip qtgraphicaleffects \
	-skip qtlocation \
	-skip qtlottie \
	-skip qtmacextras \
	-skip qtpurchasing \
	-skip qtquick3d \
	-skip qtquickcontrols \
	-skip qtquickcontrols2 \
	-skip qtquicktimeline \
	-skip qtscxml \
	-skip qtsensors \
	-skip qtspeech \
	-skip qttools \
	-skip qttranslations \
	-skip qtvirtualkeyboard \
	-skip qtwayland \
	-skip qtwebview \
	-skip qtwinextras \
	-skip qtx11extras \
	-recheck-all 
	
	echo "done..."
	popd
	
}

#编译,安装,并打包保存编译结果到挂载目录
do_make_install () {
	echo "start make and install ${PACKAGE_NAME} ..."

	if [ ! -f "${BUILD_TARGET}/${PACKAGE_NAME}.tar.gz" ]; then

		pushd ${SRC_PATH}/${PACKAGE_NAME}
		make -j $(nproc)
		make install
		echo "make and install done..."
		popd

		pushd ${BUILD_TARGET}
		tar cfz "lubancat_qt5.15.8_eglfs.tar.gz" lubancat_qt5.15.8_eglfs
		sha256sum "lubancat_qt5.15.8_eglfs.tar.gz" > "lubancat_qt5.15.8_eglfs.tar.gz.sha256"
		popd
    else
        echo "Build already exist!"
    fi
}

#删除下载的文件
do_delete_file () {
	pushd ${SRC_PATH}
	
	if [ -f "${COMPRESS_PACKAGE}" ];then
		sudo rm -f ${COMPRESS_PACKAGE}
	fi
	
	if [ -f "${COMPRESS_PACKAGE_GCC}" ];then
		sudo rm -f ${COMPRESS_PACKAGE_GCC}
	fi
	
	popd
}

# 获取Qt源码和交叉编译器并解压
fetch_src

#修理一些bug
patch_qt5

# 配置文件
do_config_before

# 配置qt编译
do_configure

# 编译，安装qt
do_make_install

# 删除文件
#do_delete_file